<?php
echo $this->Session->flash();
App::Import('model', 'EvaluationPhysiographicUnit');
App::Import('model', 'EvaluationAnnex');
?>
<?php echo $form->create('EvaluationAppraisal'); ?>
<?php echo $form->hidden("EvaluationAppraisal.id"); ?>
<?php echo $form->hidden("EvaluationAppraisal.property_id"); ?>


<?php
echo $ajax->observeField('EvaluationAppraisalDepartamentId', array(
    'url' => array('action' => 'select'),
    'frequency' => 0.2,
    'update' => 'ciudades',
    'indicator' => 'loading1'
        )
);
?>

<table id="tabla" >
    <thead>
        <tr>
            <th></th>
            <th>Dato revisión</th>
            <th>Dato avalúo</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2">ENTIDAD QUE ELABORÓ EL AVALÚO</td>

            <td>

                <?php echo $form->input("EvaluationAppraisal.entidad", array('label' => '', 'empty' => 'Seleccione entidad', 'options' => array('IGAC' => 'IGAC', 'CPR' => 'CPR'))); ?>

            </td>
        </tr>
        <tr>
            <td>Departamento</td>
            <td><?php echo $departamento ?></td>
            <td><?php
                echo $form->input('EvaluationAppraisal.departament_id', array(
                    'label' => '',
                    'empty' => __('Seleccione Departamento', true),
                ));
                ?>
            </td>
        </tr>
        <tr>
            <td>Ciudad</td>
            <td><?php echo $predio['City']['name'] ?></td>
            <td>
                <div id="ciudades">
                    <?php echo $form->input("EvaluationAppraisal.city_id", array('label' => '', 'type' => 'select', 'empty' => 'Seleccione Ciudad')); ?>
                </div>
            </td>
        </tr>
        <tr>
            <td>Vereda</td>
            <td><?php echo $predio['Property']['vereda'] ?></td>
            <td><?php echo $form->input("EvaluationAppraisal.vereda", array('label' => '')); ?></td>
        </tr>
        <tr>
            <td>Nombre del predio</td>
            <td><?php echo $predio['Property']['nombre'] ?></td>
            <td><?php echo $form->input("EvaluationAppraisal.nombre_predio", array('label' => '')); ?></td>
        </tr>
        <tr>
            <td>Matricula</td>
            <td><?php echo $predio['Property']['matricula'] ?></td>
            <td><?php echo $form->input("EvaluationAppraisal.matricula", array('label' => '')); ?></td>
        </tr>
        <tr>
            <td>Número catastral</td>
            <td><?php echo $predio['Property']['np_departamento'] . " " . $predio['Property']['np_municipio'] . " " . $predio['Property']['np_tipo'] . " " . $predio['Property']['np_sector'] . " " . $predio['Property']['np_vereda'] . " " . $predio['Property']['np_predio'] . " " . $predio['Property']['np_mejora'] ?></td>
            <td><?php echo $form->input("EvaluationAppraisal.numero_catastral", array('label' => '')); ?></td>
        </tr>
        <tr>
            <td>Área avalúo (ha)</td>
            <td></td>
            <td><?php echo $form->input("EvaluationAppraisal.area", array('label' => '')); ?></td>
        </tr>
        <tr>
            <td>Área del plano topografico</td>
            <td></td>
            <td><?php echo $areaPlanoTopografico ?></td>
        </tr>
        <tr>
            <td>Valor total avalúo comercial</td>
            <td></td>
            <td><?php echo $form->input("EvaluationAppraisal.valor", array('label' => '')); ?></td>
        </tr>
    </tbody>
</table>
<br>
<br>
<br>
<h2>DATOS UNIDADES FISIOGRÁFICAS Y ANEXOS TOTALES DEL AVALUO</h2>
<table id="tabla">
    <?php
    $unidades = new EvaluationPhysiographicUnit();
    $areaTotalUnidades = $unidades->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id']));
    $valorTotalUnidades = $unidades->field('Sum(EvaluationPhysiographicUnit.area * EvaluationPhysiographicUnit.valor_unitario)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id']));
    $anexos = new EvaluationAnnex();
    $valorTotalAnexos = $anexos->field('Sum(EvaluationAnnex.magnitud * EvaluationAnnex.valor_unitario)', array('EvaluationAnnex.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id']));
    $error = false;
    if ((round($avaluo['EvaluationAppraisal']['area'], 4) != round($areaTotalUnidades, 4) ) or empty($areaTotalUnidades)) {
        $error = true;
    }
    $error2 = false;
    if ((( round(($valorTotalUnidades + $valorTotalAnexos), 0) != round($avaluo['EvaluationAppraisal']['valor'], 0) ) or empty($valorTotalUnidades))) {
        $error2 = true;
    }
    ?>
    <tr><td <?php echo $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><b>Área registrada del avalúo (ha):</b></td><td <?php echo $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><?php echo '<b>' . number_format($avaluo['EvaluationAppraisal']['area'], 4, ',', '.') . '</b>' ?></td> </tr>
    <tr><td <?php echo $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?> ><b>Área total unidades fisiográficas (ha):</b></td><td <?php echo $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><?php echo '<b>' . number_format($areaTotalUnidades, 4, ',', '.') . '</b>'; ?></td> </tr>
    <tr><td <?php echo $error2 ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><b>Valor total anexos ($):</b></td><td <?php echo $error2 ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><?php echo '<b>' . number_format($valorTotalAnexos, 0, ',', '.') . '</b>'; ?></td> </tr>
    <tr><td <?php echo $error2 ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>   ><b>Valor total unidades fisiográficas ($):</b></td><td <?php echo $error2 ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>  ><?php echo '<b>' . number_format($valorTotalUnidades, 0, ',', '.') . '</b>'; ?></td> </tr>
    <tr><td <?php echo $error2 ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><b>Valor total avaluo ($):</b></td><td <?php echo $error2 ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><?php echo '<b>' . number_format(($valorTotalAnexos + $valorTotalUnidades), 0, ',', '.') . '</b>'; ?></td> </tr>
    <tr><td <?php echo $error2 ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>  ><b>Valor ($):</b></td><td <?php echo $error2 ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>  ><?php echo '<b>' . number_format($avaluo['EvaluationAppraisal']['valor'], 0, ',', '.') . '</b>' ?></td> </tr>
    <tr>
        <td>Concepto</td>
        <td><?php echo $form->input("EvaluationAppraisal.concepto", array('label' => '')); ?></td>
    </tr>
    <tr>
        <td>Calificación</td>
        <td><?php echo $form->input("EvaluationAppraisal.calificacion", array('label' => '', 'options' => array(0 => 'Sin registro', 1 => 'Cumple', 2 => 'Subsanable'))); ?></td>
    </tr>
</table>

<?php
if ($this->Session->read('estado') == 0)
    echo $ajax->submit('Guardar', array('url' => array('controller' => 'EvaluationAppraisals', 'action' => 'edit', $this->data['EvaluationAppraisal']['id']), 'update' => 'content', 'indicator' => 'loading'));
?>
<?php echo $form->end(); ?>
<table width="100%" border="0"  CellSpacing=10  align="center" >
    <tbody>
        <tr>
            <td><?php echo $ajax->link($html->image('regresar.gif', array('width' => '30', 'heigth' => '30', 'alt' => 'Regresar', 'title' => 'regresar')), array('controller' => 'evaluationAppraisals', 'index' => 'index', $this->data['EvaluationAppraisal']['property_id']), array('update' => 'content', 'complete' => 'cargar()', 'indicator' => 'loading', 'escape' => false)); ?></td>
        </tr>
    </tbody>
</table>